package com.venn.controller;

import com.alibaba.fastjson.JSONArray;
import com.venn.entity.ResultMessage;
import com.venn.entity.ToDoList;
import com.venn.entity.User;
import com.venn.service.ToDoListService;
import com.venn.util.CommonMethod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Venn on 2017/8/1.
 */
@Controller
@RequestMapping("/todolist")
public class ToDoListController {

    private static Logger logger = LogManager.getLogger(UserController.class.getName());
    private CommonMethod commonMethod = new CommonMethod();
    /**
     * 在service定义：不需要再写setDao的方法就可以通过接口调用Dao
     */
    @Autowired
    private ToDoListService toDoListService;

    /**
     * init open the page
     * @param userid
     * @return
     */
    @RequestMapping("/")
    public String todolist(String userid){
        return "todolist/todolist";
    }
    @RequestMapping("/bak")
    public String todolist2(String userid){
        return "todolist/todolist_back";
    }
    /**
     * query all
     * data-url : return ResultMessage
     * js : return List<ToDoList>
     * @param
     * @return
     */
    @RequestMapping(value = "/findAll")
    @ResponseBody
    public List<ToDoList> findAll(HttpSession session){
        String userId = ((User)session.getAttribute("user")).getId();
        logger.info(userId + " findAll ");
        List<ToDoList> toDoLists = toDoListService.findAll(userId);
//        ResultMessage resultMessage = new ResultMessage();
//        resultMessage.setCode("0");
//        resultMessage.setMsg("success");
//        resultMessage.setTotal(toDoLists.size());
//        resultMessage.setRows(toDoLists);
//        JSONArray jsonArray = new JSONArray();
//        jsonArray.add(resultMessage);

        return toDoLists;
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public String create(@RequestParam("content") String content,HttpSession session){
        logger.info("create todolist ");
        String userId = ((User)session.getAttribute("user")).getId();
        ToDoList toDoList = new ToDoList();
        toDoList.setUserId(userId);
        toDoList.setContent(content);
        int id = toDoListService.create(toDoList);

        Map<String, Object> result = new HashMap<String,Object>();
        result.put("id",id);

        return commonMethod.returnMsg("0","success");
    }


    @RequestMapping(value = "/finish", method = RequestMethod.POST)
    @ResponseBody
    public String finish(@RequestParam("id") String id,@RequestParam("flag") String flag){

        toDoListService.finish(id,"0".equals(flag)?"1":"0");

        return commonMethod.returnMsg("0","success");
    }
    @RequestMapping(value = "/finishSelect", method = RequestMethod.POST)
    @ResponseBody
    public String finishSelect(@RequestParam("ids") String[] ids){
        // id ids have item is empty, set "-1"
        // the id is auto increment, -1 is nothing
        for (int i=0;i<ids.length-1;i++){
            if (ids[i].isEmpty()){
                ids[i] = "-1";
            }
        }

        toDoListService.finishSelect(ids);

        return commonMethod.returnMsg("0","success");
    }
    /*
    edit the todo item
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(@RequestParam("id") String id,@RequestParam("content") String content){
        toDoListService.modify(id,content);
        ModelAndView modelAndView = new ModelAndView("todolist/edit");
        modelAndView.addObject("id",id);
        modelAndView.addObject("content",content);
        return modelAndView;
    }
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public String modify(@RequestParam("id") String id,@RequestParam("content") String content){
        toDoListService.modify(id,content);
        return commonMethod.returnMsg("0","success");
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    public String remove(@RequestParam("id") String id){
        toDoListService.remove(id);
        return commonMethod.returnMsg("0","success");
    }




}
